var ibMarketTable = {
	items: $A(),
	
	/*------------------------------*/
	/* Constructor 					*/
	init: function()
	{
		Debug.write("Initializing itemTable.js");
	},
	register: function(id)
	{
		Debug.write('ibMarket item table from' + id);
		
		$(id+"button").observe('click',this.processAddItem);
		
		ibMarketTable.buildDisplay(id);
	},
	processAddItem: function(e)
	{
		var id = e.element().identify();
		id = id.substring(0,id.lastIndexOf('button'));
		
		var itemID = parseInt($(id+"select").getValue());
		var quanity = parseInt($(id+"quanity").getValue());
		
		if(isNaN(itemID) || isNaN(quanity))
		{
			return;
		}
		
		var itemQuanity = ibMarketTable.compileHiddenInput(id);
		
		var _quanity = itemQuanity.get(itemID);
		if(Object.isUndefined(_quanity))
		{
			_quanity = 0;
		}
		_quanity += quanity;
		if(_quanity <= 0)
		{
			itemQuanity.unset(itemID);
		}
		else
		{
			itemQuanity.set(itemID, _quanity);
		}
		
		ibMarketTable.saveHiddenInput(id,itemQuanity);
		ibMarketTable.buildDisplay(id);
	},
	compileHiddenInput: function(id)
	{
		var itemQuanity = $H();
		var value = $(id).getValue();
		
		if(value == "")
		{
			return itemQuanity;
		}
		
		var items = value.split(",").each(function(v){
			var temp = v.split("=");
			var item_id = parseInt(temp[0]);
			var quanity = parseInt(temp[1]);
			if(item_id in ibMarketTable.items)
			{
				itemQuanity.set(item_id,quanity);
			}
		});
		
		return itemQuanity;
	},
	saveHiddenInput: function(id, itemQuanity)
	{
		var items = $A();
		
		itemQuanity.keys().sort(function(a,b){
			a = parseInt(a);
			b = parseInt(b);
			return a < b ? -1 : a > b ? 1 : 0;
		}).each(function(itemID){
			items.push(itemID+"="+itemQuanity.get(itemID));
		});
		
		$(id).setValue(items.join(","));
	},
	buildDisplay: function(id)
	{
		var itemQuanity = ibMarketTable.compileHiddenInput(id);
		var table = $(id+'table');
		while(table.rows.length > 0)
		{
			table.deleteRow(0);
		}
		
		var row, cell;
		
		itemQuanity.each(function(pair){
			row = table.insertRow(table.rows.length);
			row.insertCell(0).update("<strong>"+ibMarketTable.items[pair.key]+"</strong>").setStyle({
				padding: '6px 5px',
				backgroundColor: 'rgb(221, 206, 208)',
				border: '1px solid #FFFFFF'
			});
			row.insertCell(1).update(pair.value).setStyle({
				width: '60px',
				padding: '6px 5px',
				backgroundColor: 'rgb(206, 207, 221)',
				border: '1px solid #FFFFFF',
				textAlign: 'center'
			});
			row.insertCell(2).update(
			"<a href='#' style='color:red;font-weight:bold' id='"+id+"link"+pair.key+"' onclick='return false;'>[X]</a>").
			setStyle({
				width: '20px',
				padding: '6px 5px',
				backgroundColor: 'rgb(203, 216, 221)',
				border: '1px solid #FFFFFF',
				textAlign: 'right'
			});
			$(id+"link"+pair.key).observe('click',ibMarketTable.removeEntry);
		});
	},
	removeEntry: function(e)
	{
		var id = itemID = e.element().identify();
		id = id.substring(0,id.lastIndexOf('link'));
		itemID = parseInt(itemID.substr(itemID.lastIndexOf('link')+4));
		
		var itemQuanity = ibMarketTable.compileHiddenInput(id);
		
		itemQuanity.unset(itemID);
		
		ibMarketTable.saveHiddenInput(id,itemQuanity);
		ibMarketTable.buildDisplay(id);
		
		return false;
	}
}

ibMarketTable.init();